package model;

import loadsave.LoadSaveable;
import loadsave.Memento;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import loadsave.loadsaveobject.LoadSavePrimitive;

public abstract class ModelObject extends Subject implements LoadSaveable {
	
	private String name;

	/**
	 * Needed for the restoration of state from a Memento.
	 */
	public ModelObject() { }
	
	public ModelObject(String name) {
	    
		this.name = name;
	}
	
	public String getName() {
		
		return name;
	}
	
	protected void setName(String name) {
		
		this.name = name;
	}
	
	@Override
	public Memento getMemento() {
		return new Memento() {

			@Override
			public LoadSaveObject getState() {
				LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("", "ModelObject");
				toReturn.add("name", new LoadSavePrimitive("name", "String", name));
				return toReturn;
			}

			@Override
			public void restoreState(LoadSaveObject lso) {
				name = lso.get("name").getValue();
			}
			
		};
	}
}
